package com.ds.dsm.aggregation.module;

import com.ds.common.JDSException;
import com.ds.config.ResultModel;
import com.ds.dsm.aggregation.module.service.ModuleConfigService;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.FormViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.CustomModuleBean;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.TabsAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.aggregation.AggEntityConfig;
import com.ds.esd.tool.ui.component.dialog.DialogBean;
import com.ds.esd.tool.ui.enums.Dock;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(path = "/dsm/agg/view/config/win/")
@MethodChinaName(cname = "通用模块配置")
//@BottomBarMenu
@TabsAnnotation(customService = ModuleConfigService.class)
public class ModuleWinGroup {


    @CustomAnnotation(hidden = true, pid = true)
    String domainId;

    @CustomAnnotation(hidden = true, pid = true)
    String sourceClassName;

    @CustomAnnotation(hidden = true, uid = true)
    String methodName;

    public ModuleWinGroup() {

    }


    @MethodChinaName(cname = "窗口配置")
    @RequestMapping(method = RequestMethod.POST, value = "TabModuleWinView")
    @FormViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-values", caption = "窗口配置", dock = Dock.top, height = "150")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ResultModel<ModuleWinBase> getWinBaseView(String sourceClassName, String methodName, String domainId) {
        ResultModel<ModuleWinBase> result = new ResultModel<ModuleWinBase>();
        try {
            AggEntityConfig classConfig = DSMFactory.getInstance().getAggregationManager().getAggEntityConfig(sourceClassName, domainId);
            MethodConfig methodAPIBean = classConfig.getMethodByName(methodName);
            CustomModuleBean customModuleBean = methodAPIBean.getModuleBean();
            if (customModuleBean == null) {
                customModuleBean = new CustomModuleBean();
            }
            result.setData(new ModuleWinBase(customModuleBean));
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "TabModuleWinMove")
    @FormViewAnnotation
    @APIEventAnnotation(autoRun = true)
    @ModuleAnnotation(dock = Dock.fill, imageClass = "spafont spa-icon-rendermode", caption = "控制配置")
    @ResponseBody
    public ResultModel<ModuleWinMove> getModuleWinMove(String sourceClassName, String methodName, String domainId) {
        ResultModel<ModuleWinMove> result = new ResultModel<ModuleWinMove>();
        try {
            AggEntityConfig classConfig = DSMFactory.getInstance().getAggregationManager().getAggEntityConfig(sourceClassName, domainId);
            MethodConfig methodAPIBean = classConfig.getMethodByName(methodName);
            CustomModuleBean customModuleBean = methodAPIBean.getModuleBean();
            DialogBean dialogBean = methodAPIBean.getDialogBean();
            result.setData(new ModuleWinMove(customModuleBean, dialogBean));
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(method = RequestMethod.POST, value = "TabModuleWinBar")
    @FormViewAnnotation
    @APIEventAnnotation(autoRun = true)
    @ModuleAnnotation(dock = Dock.bottom, imageClass = "spafont spa-icon-rendermode", height = "150", caption = "功能按钮")
    @ResponseBody
    public ResultModel<ModuleWinBar> getModuleWinBar(String sourceClassName, String methodName, String domainId) {
        ResultModel<ModuleWinBar> result = new ResultModel<ModuleWinBar>();

        try {
            AggEntityConfig classConfig = DSMFactory.getInstance().getAggregationManager().getAggEntityConfig(sourceClassName, domainId);
            MethodConfig methodAPIBean = classConfig.getMethodByName(methodName);
            DialogBean dialogBean = methodAPIBean.getDialogBean();
            if (dialogBean == null) {
                dialogBean = new DialogBean();
            }
            result.setData(new ModuleWinBar(dialogBean));
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return result;
    }


    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getDomainId() {
        return domainId;
    }

    public void setDomainId(String domainId) {
        this.domainId = domainId;
    }

    public String getSourceClassName() {
        return sourceClassName;
    }

    public void setSourceClassName(String sourceClassName) {
        this.sourceClassName = sourceClassName;
    }
}
